# coding=utf-8
#

import volcenginesdkecs
import volcenginesdkcore
from volcenginesdkcore.rest import ApiException


def get_instances(zone_id: str):
    """
    获取实例列表
    """

    configuration = volcenginesdkcore.Configuration()
    configuration.ak = "AKLTZWZjZDE5ZjUwZTNkNDFiM2JiOGVlYjE2ZWFlODBmMmY"
    configuration.sk = "TXpjMU4ySmtZV1V4TnpKaE5HRTNZMkpoTWpVMk9EYzVNelk0TVdFMk1Eaw=="
    configuration.region = "cn-datong"

    taskid = "cmd-yd8cey8m5ibwar417yzs"

    # set default configuration
    volcenginesdkcore.Configuration.set_default(configuration)
    api_instance = volcenginesdkecs.ECSApi()

    data = {"zone_id": zone_id, "max_results": 100}

    instance_ids = []

    des_request = volcenginesdkecs.DescribeInstancesRequest(**data)
    resp = api_instance.describe_instances(des_request)
    instance_ids = [x.instance_id for x in resp.instances]

    #task_data = {"command_id": taskid, "frequency": "1m", "instance_ids": "&".join(instance_ids)}
    task_data = {"command_id": taskid, "frequency": "1m", "instance_ids": instance_ids}
    print(task_data)
    import ipdb;ipdb.set_trace()
    task_request = volcenginesdkecs.InvokeCommandRequest(task_data)
    resp = api_instance.invoke_command(task_request)
    import ipdb;ipdb.set_trace()
    print("foo")


def restart_agent(sus_agent_list: list):
    """ """
    configuration = volcenginesdkcore.Configuration()
    configuration.ak = "AKLTZWZjZDE5ZjUwZTNkNDFiM2JiOGVlYjE2ZWFlODBmMmY"
    configuration.sk = "TXpjMU4ySmtZV1V4TnpKaE5HRTNZMkpoTWpVMk9EYzVNelk0TVdFMk1Eaw=="
    configuration.region = "cn-datong"
    # set default configuration
    volcenginesdkcore.Configuration.set_default(configuration)
    api_instance = volcenginesdkecs.ECSApi()

    data = {"instance_ids": sus_agent_list, "force_stop": True}

    #start_instance_request = volcenginesdkecs.CreateScheduledInstancesRequest(**data)
    reboot_instance_request = volcenginesdkecs.RebootInstancesRequest(**data)
    resp = api_instance.reboot_instances(reboot_instance_request)
    print(resp)


if __name__ == "__main__":
    zone_id = "cn-datong-a"

    agent_list = ["i-yd8c7ssb9cwnd1mqfhqh"]
    #get_instances(zone_id)
    restart_agent(agent_list)
